package com.komodo.island.gdx.game.sandcastle;

import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.actors.Image;
import com.komodo.island.gdx.Art;
import com.komodo.island.gdx.Constants;
import com.komodo.island.gdx.KomodoIsland;
import com.komodo.island.gdx.Persistence;
import com.komodo.island.gdx.dialog.LoseDialog;
import com.komodo.island.gdx.dialog.WinDialog;
import com.komodo.island.gdx.game.Time;
import com.komodo.island.gdx.screen.GameScreen;
import com.komodo.island.gdx.screen.Screen;

public class SandCastle extends GameScreen {
	public static final int UNTOUCHED = 9999;
	public static final int TOP = 0;
	public static final int MIDDLE = 1;
	public static final int BOTTOM = 2;
	public static int PLAYER_PICTURE_WIDTH = Constants.GAME_WIDTH / 2;
	public static int RIGHT_PICTURE_WIDTH = Constants.GAME_WIDTH / 4;
	public static final int RIGHT_REGION_PICTURE_WIDTH = 200;
	public static final int RIGHT_REGION_PICTURE_HEIGHT = 100;

	public int[] playerPicture = new int[3];
	public float[] nowPicture = new float[3];
	public int rightPicture;
	public int touchedTop;
	public int touchedMiddle;
	public int touchedBottom;
	// public int score;
	public Random rand;
	public TextureRegion castleTopPart;
	public TextureRegion castleMiddlePart;
	public TextureRegion castleBottomPart;
	public TextureRegion castleTopPart2;
	public TextureRegion castleMiddlePart2;
	public TextureRegion castleBottomPart2;
	public TextureRegion castleFull;
	public TextureRegion backGroundPicture;
	public TextureRegion target;
	public Texture sandCastleTexture;
	public Time time;
	public double second;

	boolean isDelay = false;
	double delay;
	double currdelay;

	public Sound swype;
	public Sound done;

	public void init(KomodoIsland komodoIsland, Texture texture) {
		// TODO Auto-generated method stub
		super.init(komodoIsland, texture);

		previousScreen = komodoIsland.touristScreen;

		if (rand == null)
			rand = new Random();
		if (time == null)
			time = new Time(1, 0);
	}

	@Override
	protected void onGameLoading() {
		// TODO Auto-generated method stub
		if (swype == null) {
			swype = Gdx.audio.newSound(Gdx.files
					.internal("sfx/castle/swype.wav"));
		}
		if (done == null) {
			done = Gdx.audio.newSound(Gdx.files
					.internal("sfx/castle/score.mp3"));
		}
		if (komodoIsland.castle == null) {
			komodoIsland.castle = Gdx.audio.newMusic(Gdx.files
					.internal("bgm/castle.mp3"));
			komodoIsland.castle.setLooping(true);
			komodoIsland.castle.setVolume(0.5f);
		}

		if (Persistence.sound) {
			try {
				if (!komodoIsland.castle.isPlaying()) {
					komodoIsland.castle.play();
				}
			} catch (Exception e) {

			}
		}

		if (sandCastleTexture == null)
			sandCastleTexture = new Texture(Gdx.files.internal("castle1.png"));

		if (loseDialog == null)
			this.loseDialog = new SandCastleLoseDialog(480, 320, true, this,
					komodoIsland.dialogTexture,komodoIsland);
		if (winDialog == null)
			this.winDialog = new SandCastleWinDialog(480, 320, true, this,
					komodoIsland.dialogTexture,komodoIsland);

		camera = new OrthographicCamera(480, 320);
	}

	@Override
	protected void onGameLose() {
		// TODO Auto-generated method stub
		// Art.drawString("You Lose", 0, 320 - 32);
	}

	@Override
	protected void onGamePause() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onGameRunning() {
		// TODO Auto-generated method stub
		camera.update();
		batch.setProjectionMatrix(camera.combined);

		time.runTime();
		if (60 - time.getSecond() < 0.2) {
			second = 60;
		}

		if (Math.ceil(time.getSecond()) != 0 || time.getMinute() != 0) {
			if (nowPicture[0] - playerPicture[0] > 0.1) {
				if (second - time.getSecond() >= 0.09) {
					second = time.getSecond();
					nowPicture[0] -= 0.333333;
				}
			} else if (nowPicture[0] - playerPicture[0] < -0.1) {
				if (second - time.getSecond() >= 0.09) {
					second = time.getSecond();
					nowPicture[0] += 0.33333;
				}
			} else if (nowPicture[1] - playerPicture[1] > 0.1) {
				if (second - time.getSecond() >= 0.09) {
					second = time.getSecond();
					nowPicture[1] -= 0.33333;
				}
			} else if (nowPicture[1] - playerPicture[1] < -0.1) {
				if (second - time.getSecond() >= 0.09) {
					second = time.getSecond();
					nowPicture[1] += 0.33333;
				}
			} else if (nowPicture[2] - playerPicture[2] > 0.1) {
				if (second - time.getSecond() >= 0.09) {
					second = time.getSecond();
					nowPicture[2] -= 0.33333;
				}
			} else if (nowPicture[2] - playerPicture[2] < -0.1) {
				if (second - time.getSecond() >= 0.09) {
					second = time.getSecond();
					nowPicture[2] += 0.33333;
				}
			} else {
				// User input handler
				if (Gdx.input.isTouched()) {
					if (Gdx.input.justTouched()) {
						if (isTopTouched()) {
							if (Persistence.sound)
								swype.play(1);
							touchedTop = Gdx.input.getX();
						} else if (isMiddleTouched()) {
							if (Persistence.sound)
								swype.play(1);
							touchedMiddle = Gdx.input.getX();
						} else if (isBottomTouched()) {
							if (Persistence.sound)
								swype.play(1);
							touchedBottom = Gdx.input.getX();
						}
					} else {
						
						if (!isTopTouched()) {
							touchedTop = UNTOUCHED;
						} else if (!isMiddleTouched()) {
							touchedMiddle = UNTOUCHED;
						} else if (!isBottomTouched()) {
							touchedBottom = UNTOUCHED;
						}

						if (touchedTop != UNTOUCHED) {
							if (Gdx.input.getX() > touchedTop) {
								playerPicture[TOP]--;
								if (playerPicture[TOP] == -1) {
									playerPicture[TOP] = 9;
									nowPicture[TOP] = 10;
								}
								touchedTop = UNTOUCHED;
							} else if (Gdx.input.getX() < touchedTop) {
								playerPicture[TOP]++;
								if (playerPicture[TOP] == 10) {
									playerPicture[TOP] = 0;
									nowPicture[TOP] = -1;
								}
								touchedTop = UNTOUCHED;
							}
						} else if (touchedMiddle != UNTOUCHED) {
							if (Gdx.input.getX() > touchedMiddle) {
								playerPicture[MIDDLE]--;
								if (playerPicture[MIDDLE] == -1) {
									playerPicture[MIDDLE] = 9;
									nowPicture[MIDDLE] = 10;
								}
								touchedMiddle = UNTOUCHED;
							} else if (Gdx.input.getX() < touchedMiddle) {
								playerPicture[MIDDLE]++;
								if (playerPicture[MIDDLE] == 10) {
									playerPicture[MIDDLE] = 0;
									nowPicture[MIDDLE] = -1;
								}
								touchedMiddle = UNTOUCHED;
							}
						} else if (touchedBottom != UNTOUCHED) {
							if (Gdx.input.getX() > touchedBottom) {
								playerPicture[BOTTOM]--;
								if (playerPicture[BOTTOM] == -1) {
									playerPicture[BOTTOM] = 9;
									nowPicture[BOTTOM] = 10;
								}
								touchedBottom = UNTOUCHED;
							} else if (Gdx.input.getX() < touchedBottom) {
								playerPicture[BOTTOM]++;
								if (playerPicture[BOTTOM] == 10) {
									playerPicture[BOTTOM] = 0;
									nowPicture[BOTTOM] = -1;
								}
								touchedBottom = UNTOUCHED;
							}
						}
					}
				} else {
					touchedBottom = UNTOUCHED;
					touchedMiddle = UNTOUCHED;
					touchedTop = UNTOUCHED;
				}
			}
			// Set Picture
			batch.begin();

			// TOP
			if (Math.abs(nowPicture[TOP] - playerPicture[TOP]) > 0.1) {
				if (playerPicture[TOP] >= 0 && playerPicture[TOP] <= 9
						&& nowPicture[TOP] >= 0 && nowPicture[TOP] <= 9) {
					batch.draw(castleTopPart,
							120 - (PLAYER_PICTURE_WIDTH * nowPicture[TOP]),
							Constants.GAME_HEIGHT / 6 * 3,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
					batch.draw(
							castleTopPart2,
							120 - (PLAYER_PICTURE_WIDTH * (nowPicture[TOP] - 5)),
							Constants.GAME_HEIGHT / 6 * 3,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
				} else if (nowPicture[TOP] < 0) {
					batch.draw(castleTopPart,
							120 - (PLAYER_PICTURE_WIDTH * (nowPicture[TOP])),
							Constants.GAME_HEIGHT / 6 * 3,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
					batch.draw(
							castleTopPart2,
							120 - (PLAYER_PICTURE_WIDTH * (nowPicture[TOP] + 5)),
							Constants.GAME_HEIGHT / 6 * 3,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
				} else if (nowPicture[TOP] > 9) {
					batch.draw(
							castleTopPart,
							120 - (PLAYER_PICTURE_WIDTH * (nowPicture[TOP] - 10)),
							Constants.GAME_HEIGHT / 6 * 3,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
					batch.draw(
							castleTopPart2,
							120 - (PLAYER_PICTURE_WIDTH * (nowPicture[TOP] - 5)),
							Constants.GAME_HEIGHT / 6 * 3,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
				}

			} else {
				batch.draw(castleTopPart,
						120 - (PLAYER_PICTURE_WIDTH * playerPicture[TOP]),
						Constants.GAME_HEIGHT / 6 * 3,
						PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
				batch.draw(
						castleTopPart2,
						120 - (PLAYER_PICTURE_WIDTH * (playerPicture[TOP] - 5)),
						Constants.GAME_HEIGHT / 6 * 3,
						PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
			}
			// MIDDLE
			if (Math.abs(nowPicture[MIDDLE] - playerPicture[MIDDLE]) > 0.1) {
				if (playerPicture[MIDDLE] >= 0 && playerPicture[MIDDLE] <= 9
						&& nowPicture[MIDDLE] >= 0 && nowPicture[MIDDLE] <= 9) {
					batch.draw(castleMiddlePart,
							120 - (PLAYER_PICTURE_WIDTH * nowPicture[MIDDLE]),
							Constants.GAME_HEIGHT / 6 * 2,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
					batch.draw(
							castleMiddlePart2,
							120 - (PLAYER_PICTURE_WIDTH * (nowPicture[MIDDLE] - 5)),
							Constants.GAME_HEIGHT / 6 * 2,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
				} else if (nowPicture[MIDDLE] < 0) {
					batch.draw(castleMiddlePart,
							120 - (PLAYER_PICTURE_WIDTH * nowPicture[MIDDLE]),
							Constants.GAME_HEIGHT / 6 * 2,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
					batch.draw(
							castleMiddlePart2,
							120 - (PLAYER_PICTURE_WIDTH * (nowPicture[MIDDLE] + 5)),
							Constants.GAME_HEIGHT / 6 * 2,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
				} else if (nowPicture[MIDDLE] > 9) {
					batch.draw(
							castleMiddlePart,
							120 - (PLAYER_PICTURE_WIDTH * (nowPicture[MIDDLE] - 10)),
							Constants.GAME_HEIGHT / 6 * 2,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
					batch.draw(
							castleMiddlePart2,
							120 - (PLAYER_PICTURE_WIDTH * (nowPicture[MIDDLE] - 5)),
							Constants.GAME_HEIGHT / 6 * 2,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
				}
			} else {
				batch.draw(castleMiddlePart,
						120 - (PLAYER_PICTURE_WIDTH * playerPicture[MIDDLE]),
						Constants.GAME_HEIGHT / 6 * 2,
						PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
				batch.draw(
						castleMiddlePart2,
						120 - (PLAYER_PICTURE_WIDTH * (playerPicture[MIDDLE] - 5)),
						Constants.GAME_HEIGHT / 6 * 2,
						PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
			}
			// BOTTOM
			if (Math.abs(nowPicture[BOTTOM] - playerPicture[BOTTOM]) > 0.1) {
//				System.out.println("BOTTOM");
				if (playerPicture[BOTTOM] >= 0 && playerPicture[BOTTOM] <= 9
						&& nowPicture[BOTTOM] >= 0 && nowPicture[BOTTOM] <= 9) {
					batch.draw(castleBottomPart,
							120 - (PLAYER_PICTURE_WIDTH * nowPicture[BOTTOM]),
							Constants.GAME_HEIGHT / 6,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
					batch.draw(
							castleBottomPart2,
							120 - (PLAYER_PICTURE_WIDTH * (nowPicture[BOTTOM] - 5)),
							Constants.GAME_HEIGHT / 6,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
				} else if (nowPicture[BOTTOM] < 0) {
					batch.draw(castleBottomPart,
							120 - (PLAYER_PICTURE_WIDTH * nowPicture[BOTTOM]),
							Constants.GAME_HEIGHT / 6,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
					batch.draw(
							castleBottomPart2,
							120 - (PLAYER_PICTURE_WIDTH * (nowPicture[BOTTOM] + 5)),
							Constants.GAME_HEIGHT / 6,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
				} else if (nowPicture[BOTTOM] > 9) {
					batch.draw(
							castleBottomPart,
							120 - (PLAYER_PICTURE_WIDTH * (nowPicture[BOTTOM] - 10)),
							Constants.GAME_HEIGHT / 6,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
					batch.draw(
							castleBottomPart2,
							120 - (PLAYER_PICTURE_WIDTH * (nowPicture[BOTTOM] - 5)),
							Constants.GAME_HEIGHT / 6,
							PLAYER_PICTURE_WIDTH * 5, Constants.GAME_HEIGHT / 6);
				}
			} else {
				batch.draw(castleBottomPart,
						120 - (PLAYER_PICTURE_WIDTH * playerPicture[BOTTOM]),
						Constants.GAME_HEIGHT / 6, PLAYER_PICTURE_WIDTH * 5,
						Constants.GAME_HEIGHT / 6);
				batch.draw(
						castleBottomPart2,
						120 - (PLAYER_PICTURE_WIDTH * (playerPicture[BOTTOM] - 5)),
						Constants.GAME_HEIGHT / 6, PLAYER_PICTURE_WIDTH * 5,
						Constants.GAME_HEIGHT / 6);
			}

			batch.draw(backGroundPicture, 0, 0, Constants.GAME_WIDTH,
					Constants.GAME_HEIGHT);

			batch.draw(castleFull,
					(Constants.GAME_WIDTH - RIGHT_REGION_PICTURE_WIDTH) / 2,
					Constants.GAME_HEIGHT - RIGHT_REGION_PICTURE_HEIGHT,
					RIGHT_REGION_PICTURE_WIDTH, RIGHT_REGION_PICTURE_HEIGHT);

			batch.draw(
					target,
					(Constants.GAME_WIDTH - (target.getRegionWidth() / 2)) / 2,
					(Constants.GAME_HEIGHT - RIGHT_REGION_PICTURE_HEIGHT - (target
							.getRegionHeight() / 3)),
					target.getRegionWidth() / 2, target.getRegionHeight() / 2);

			batch.end();

			Art.drawString("Time : " + Art.decimal2.format(time.getSecond()),
					0, -2);
			Art.drawString("Score : " + score, 0, 320 - 32);

			// isPictureSame
			if (isPictureMatch()) {
				//
				
					if (isDelay) {
						currdelay = System.currentTimeMillis();
						if (currdelay - delay >= 1000) {
							isDelay = false;
							startTime = System.currentTimeMillis();
							gameState = GAME_START_ANIMATION;

							
							if (Persistence.sound)
								done.play(1);
							
							
							nextPicture();
						}
					} else {
						isDelay = true;
						delay = System.currentTimeMillis();
					}
			}
		} else {
			if (score >= 1500) {
				setGameToWin();
			} else {
				setGameToLose();
			}
		}
	}

	@Override
	protected void onGameStart() {
		// TODO A uto-generated method stub
		time.setMinute(1);
		time.setSecond(0);
		score = 0;
		second = 0;
		touchedTop = 9999;
		touchedMiddle = 9999;
		touchedBottom = 9999;
		playerPicture[TOP] = rand.nextInt(4);
		playerPicture[MIDDLE] = rand.nextInt(4);
		playerPicture[BOTTOM] = rand.nextInt(4);
		nowPicture[TOP] = playerPicture[TOP];
		nowPicture[MIDDLE] = playerPicture[MIDDLE];
		nowPicture[BOTTOM] = playerPicture[BOTTOM];
		rightPicture = rand.nextInt(4);

		castleTopPart = new TextureRegion(sandCastleTexture, 480, 0, 1000, 33);
		castleMiddlePart = new TextureRegion(sandCastleTexture, 480, 33, 1000,
				33);
		castleBottomPart = new TextureRegion(sandCastleTexture, 480, 66, 1000,
				34);
		castleTopPart2 = new TextureRegion(sandCastleTexture, 480, 100, 1000,
				33);
		castleMiddlePart2 = new TextureRegion(sandCastleTexture, 480, 133,
				1000, 33);
		castleBottomPart2 = new TextureRegion(sandCastleTexture, 480, 166,
				1000, 34);
		castleFull = new TextureRegion(sandCastleTexture,
				480 + (RIGHT_REGION_PICTURE_WIDTH * (rightPicture % 5)),
				RIGHT_REGION_PICTURE_HEIGHT * (rightPicture / 5),
				RIGHT_REGION_PICTURE_WIDTH, RIGHT_REGION_PICTURE_HEIGHT);
		backGroundPicture = new TextureRegion(sandCastleTexture, 0, 0, 480, 320);
		target = new TextureRegion(sandCastleTexture, 0, 480, 125, 30);
	}

	@Override
	protected void onGameWin() {
		// TODO Auto-generated method stub
		// System.out.println("win");
		// Art.drawString("Win!!", 0, 320 - 32);
	}

	protected void nextPicture() {
		score += 500;
		startTime = System.currentTimeMillis();
		rightPicture = rand.nextInt(9);
		playerPicture[0] = rand.nextInt(9);
		playerPicture[1] = rand.nextInt(9);
		playerPicture[2] = rand.nextInt(9);
		nowPicture[0] = playerPicture[0];
		nowPicture[1] = playerPicture[1];
		nowPicture[2] = playerPicture[2];
		castleFull.setRegion(
				480 + (RIGHT_REGION_PICTURE_WIDTH * (rightPicture % 5)),
				RIGHT_REGION_PICTURE_HEIGHT * (rightPicture / 5),
				RIGHT_REGION_PICTURE_WIDTH, RIGHT_REGION_PICTURE_HEIGHT);

	}

	public void checkPicture() {
		if (isPictureMatch()) {
			// score++;

			if (isDelay) {
				currdelay = System.currentTimeMillis();
				if (currdelay - delay >= 1000) {
					isDelay = false;

				}
			} else {
				isDelay = true;
				delay = System.currentTimeMillis();
			}
//			System.out.println(score);

		} else {

		}
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub
		super.resize(arg0, arg1);
		// camera = new OrthographicCamera(arg0, arg1);
		// camera.position.set(Gdx.graphics.getWidth() / 2,
		// Gdx.graphics.getHeight() / 2, 0);

	}

	public boolean isTopTouched() {
		return (Gdx.input.getX() >= Gdx.graphics.getWidth() / 4
				&& Gdx.input.getX() <= Gdx.graphics.getWidth() / 4 * 3
				&& Gdx.input.getY() >= Gdx.graphics.getHeight() / 6 * 2 && Gdx.input
				.getY() <= Gdx.graphics.getHeight() / 6 * 3);
	}

	public boolean isMiddleTouched() {
		return (Gdx.input.getX() >= Gdx.graphics.getWidth() / 4
				&& Gdx.input.getX() <= Gdx.graphics.getWidth() / 4 * 3
				&& Gdx.input.getY() >= Gdx.graphics.getHeight() / 6 * 3 && Gdx.input
				.getY() <= Gdx.graphics.getHeight() / 6 * 4);
	}

	public boolean isBottomTouched() {
		return (Gdx.input.getX() >= Gdx.graphics.getWidth() / 4
				&& Gdx.input.getX() <= Gdx.graphics.getWidth() / 4 * 3
				&& Gdx.input.getY() >= Gdx.graphics.getHeight() / 6 * 4 && Gdx.input
				.getY() <= Gdx.graphics.getHeight() / 6 * 5);
	}

	public boolean isPictureMatch() {
		return (playerPicture[0] == rightPicture
				&& playerPicture[1] == rightPicture && playerPicture[2] == rightPicture);
	}

	// @Override
	// protected boolean onGameStartAnimation() {
	// // TODO Auto-generated method stub
	// currTime = System.currentTimeMillis();
	// if (currTime - startTime <= 1000) {
	// Art.drawString("3", 240 - 32, 160 - 32);
	// return true;
	// } else if (currTime - startTime > 1000 && currTime - startTime <= 2000) {
	// Art.drawString("2", 240 - 32, 160 - 32);
	// return true;
	// } else if (currTime - startTime > 2000 && currTime - startTime <= 3000) {
	// Art.drawString("1", 240 - 32, 160 - 32);
	// return true;
	// } else {
	// if (score > 0)
	// nextPicture();
	// return false;
	// }
	//
	// }

	class SandCastleLoseDialog extends LoseDialog {

		public Image finish;
		
		public SandCastleLoseDialog(float width, float height, boolean stretch,
				Screen screen, Texture texture,KomodoIsland komodoIsland) {
			super(width, height, stretch, screen, texture,komodoIsland);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void init() {
			// TODO Auto-generated method stub
			super.init();
			finish = new Image("finish", new TextureRegion(
					screen.komodoIsland.additionTexture, 192+96, 856, 96, 96));
		}

		@Override
		public void addElements() {
			// TODO Auto-generated method stub
			super.addElements();
			addActor(finish);
		}

		@Override
		public void setThePosition() {
			// TODO Auto-generated method stub
			super.setThePosition();
			finish.x = siKomo.x;
			finish.y = siKomo.y;

		}
	}

	class SandCastleWinDialog extends WinDialog {

		public Image finish;
		
		public SandCastleWinDialog(float width, float height, boolean stretch,
				Screen screen, Texture texture,KomodoIsland komodoIsland) {
			super(width, height, stretch, screen, texture,komodoIsland);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void init() {
			// TODO Auto-generated method stub
			super.init();
			finish = new Image("finish", new TextureRegion(
					screen.komodoIsland.additionTexture, 192, 856, 96, 96));
		}

		@Override
		public void addElements() {
			// TODO Auto-generated method stub
			super.addElements();
			addActor(finish);
		}

		@Override
		public void setThePosition() {
			// TODO Auto-generated method stub
			super.setThePosition();
			finish.x = siKomo.x;
			finish.y = siKomo.y;

		}

	}

	@Override
	public void setCameraToDefault() {
		// TODO Auto-generated method stub
		camera.viewportWidth = Constants.GAME_WIDTH;
		camera.viewportHeight = Constants.GAME_HEIGHT;

		camera.position.set(Constants.GAME_WIDTH / 2,
				Constants.GAME_HEIGHT / 2, 0);
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();

	}

	@Override
	public void cleanResScreen() {
		// TODO Auto-generated method stub
		super.cleanResScreen();
		try {
			sandCastleTexture.dispose();
			swype.dispose();
			done.dispose();
		} catch (Exception e) {

		}
	}
	
	@Override
	public void exitScreen() {
		// TODO Auto-generated method stub
		try {
			sandCastleTexture.dispose();
			swype.dispose();
			done.dispose();
			sandCastleTexture=null;
			swype = null;
			done =null;
		} catch (Exception e) {

		}
		super.exitScreen();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
		try {
			if (Persistence.sound) {
				if (komodoIsland.castle.isPlaying()) {
					komodoIsland.castle.stop();
				}
			} else {

			}
		} catch (Exception e) {

		}
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		try {
			if (Persistence.sound) {
				if (!komodoIsland.castle.isPlaying()) {
					komodoIsland.castle.play();
				}
			} else {

			}
		} catch (Exception e) {

		}
	}
}
